﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.SessionState;
using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// Register 的摘要说明
    /// </summary>
    public class Register : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info=new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string username = context.Request["username"];
                string usernikename = context.Request["usernikename"];
                string password = context.Request["password"];
                string email = context.Request["email"];
                string phone = context.Request["phone"];
                string captcha=context.Request["captcha"];

                if (captcha != context.Session["Captcha"] + "")
                {
                    info.Success = false;
                    info.Tip = "captcha";
                }
                else
                {
                    User_BLL bll = new User_BLL();
                    USER user = bll.GetModelBySQL(string.Format("select USER_ID,USER_NAME,USER_NIKENAME,PASSWORD,EMAIL,TELEPHONE,ENABLE,USER_HEAD_IMG from [USER] where USER_NAME='{0}'", username));

                    if (user != null)
                    {
                        info.Success = false;
                        info.Tip = "username";
                    }
                    else
                    {
                        user = new USER()
                         {
                             USER_ID = Util.GetUID(),
                             USER_NAME = username,
                             USER_NIKENAME = usernikename,
                             PASSWORD = Tom.Net.Security.EncryptUtil.EncryptDES(password, "Omabar"),
                             EMAIL = email,
                             TELEPHONE = phone,
                             ENABLE = 1,
                             USER_HEAD_IMG = AppConst.Domain + "Images / cover_default.jpg"
                         };
                        info = bll.Add(user);

                        info.Url = "Index.aspx";

                        if (info.Success)
                            context.Session["CURRENTUSER"] = user;
                    }
                }
            }
            catch (Exception ex)
            {
                info.Success = false;
                info.Tip = "错误";
                info.Url = "Register.aspx";
                Tom.Net.Log.LogManager.Error("注册失败", ex);
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}